home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / ActiveX SDK / Container Common / Util.cpp < prev    next >
C/C++ Source or Header  |  1997-01-03  |  2KB  |  84 lines

  1. #include "headers.h"
  2. #include "Util.h"
  3.  
  4. Handle FileURLFromFileSpec (FSSpec* FileSpec)
  5. {
  6.     CInfoPBRec    myPB;            // parameter block for PBGetCatInfo
  7.     Str255        dirName;        // a directory name
  8.     OSErr        myErr;
  9.     Int32        NameLen = 0;
  10.     Int32        PathLen = 256;
  11.     Handle        tempPathHdl = NewHandleClear(256);
  12.     char*        tempPath;
  13.     char        FileURL[16] = "file:///";
  14.     Int16        move;
  15.  
  16.     if(tempPathHdl == NULL)
  17.         return NULL;
  18.     
  19.     HLock(tempPathHdl);
  20.     tempPath = *tempPathHdl;
  21.         
  22.     tempPath[0] = 0;                // initialize full pathname}
  23.     myPB.hFileInfo.ioNamePtr = &dirName[0];
  24.     myPB.hFileInfo.ioVRefNum = FileSpec->vRefNum;    // indicate target volume
  25.     myPB.dirInfo.ioDrParID = FileSpec->parID;      // initialize parent directory ID
  26.     myPB.dirInfo.ioFDirIndex = -1;                    // get info about a directory
  27.  
  28.     p2cstr(FileSpec->name);
  29.     strcat(tempPath, (char*)FileSpec->name);
  30.     c2pstr((char*)FileSpec->name);
  31.     NameLen = strlen(tempPath);
  32.     // Get name of each parent directory, up to root directory.
  33.     do
  34.     {
  35.         myPB.dirInfo.ioDrDirID = myPB.dirInfo.ioDrParID;
  36.         myErr = PBGetCatInfoSync(&myPB);
  37.         p2cstr(dirName);
  38.         
  39.         // make sur string is long enough
  40.         while((strlen((char*)dirName) + NameLen) > (PathLen + 2))    // room for name, colon and termintating 0
  41.         {
  42.             PathLen += 256;
  43.             HUnlock(tempPathHdl);
  44.             SetHandleSize(tempPathHdl, PathLen);
  45.             if(tempPathHdl == NULL)
  46.                 return NULL;
  47.                 
  48.             HLock(tempPathHdl);
  49.             tempPath = *tempPathHdl;
  50.             
  51.         }
  52.         move = strlen((char*)dirName) + 1;
  53.         
  54.         // move existing string to make room for new folder name and ":"
  55.         BlockMove(tempPath, (char*)&tempPath[move], NameLen);
  56.         strcat((char*)dirName, "/");
  57.         move = strlen((char*)dirName);
  58.         BlockMove(dirName, tempPath, move);
  59.         NameLen += strlen((char*)dirName);
  60.     }
  61.     while( myPB.dirInfo.ioDrDirID != fsRtDirID);
  62.     
  63.     // add the file URL prefix
  64.     move = strlen((char*) FileURL);
  65.     
  66.     if ( (NameLen + move) > GetHandleSize(tempPathHdl) )
  67.     {
  68.         HUnlock(tempPathHdl);
  69.         SetHandleSize(tempPathHdl, GetHandleSize(tempPathHdl) + 256);
  70.         if(tempPathHdl == NULL)
  71.             return NULL;
  72.         HLock(tempPathHdl);
  73.         tempPath = *tempPathHdl;
  74.     }
  75.     
  76.     // move existing string to make room for new folder name and ":"
  77.     BlockMove(tempPath, (char*)&tempPath[move], NameLen);
  78.     BlockMove(FileURL, tempPath, move);
  79.     
  80.     HUnlock(tempPathHdl);
  81.     
  82.     return tempPathHdl;
  83. }
  84.